//
//  UINavigationBar+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

public extension UINavigationBar {
    
    /// 导航栏通用设置
    /// - Parameters:
    ///   - titleColor: title字体颜色
    ///   - titleFont: title字体大小
    ///   - backColor: 导航栏的背景色
    ///   - lineColor: 导航栏线条颜色
    func setNavigationBarStyle(titleColor: UIColor? = nil,
                               titleFont: UIFont? = nil,
                               navBackground: UIColor? = nil,
                               lineColor: UIColor? = nil) {
        
        // 修改导航栏字体大小/景色
        let titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor ?? Color.hex_3,
                                   NSAttributedString.Key.font: titleColor ?? Font.dpBold(20)]
        UINavigationBar.appearance().isTranslucent = false
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            // 重置背景和阴影颜色
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = navBackground ?? Color.hex_3
            appearance.titleTextAttributes = titleTextAttributes
            let size = CGSize(width: Device_y.screenWidth, height: 1)
            appearance.shadowImage = UIImage().creatImageWithColor(color: lineColor ?? Color.hex_e5,
                                                                   size: size)
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
            UINavigationBar.appearance().backgroundColor = navBackground ?? Color.hex_e5
        } else {
            UINavigationBar.appearance().barTintColor = navBackground ?? Color.hex_e5
            UINavigationBar.appearance().titleTextAttributes = titleTextAttributes
            setBarBottomLine(color: lineColor ?? .white)
        }
        
    }
    
    /// 设置下影线
    func setBarBottomLine(color: UIColor) {
        shadowImage = UIImage(color: color, size: CGSize.init(width: Device_y.screenWidth, height: 0.05))
    }
    
}

